<!--
Copyright 2013 The Polymer Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<!--
/**
 * @module Polymer Elements
 */
/**
 * polymer-localstorage provides access to localStorage.  The "name" property
 * is the key to the data ("value" property) stored in localStorage.
 *
 * Example:
 *
 *     <polymer-localstorage name="my-app-storage" value="{{value}}"></polymer-localstorage>
 *
 * @class polymer-localstorage
 */
-->
<polymer-element name="polymer-localstorage" attributes="name value useRaw">
  <template>
    <style>
      @host {
        * {
          display: none;
        }
      }
    </style>
  </template>
  <script>
    Polymer('polymer-localstorage', {
      /**
       * The key to the data stored in localStorage.
       *
       * @attribute name
       * @type string
       * @default null
       */
      /**
       * The data associated with the specified name.
       *
       * @attribute value
       * @type object
       * @default null
       */
      /**
       * If true, the value is stored and retrieved without JSON processing.
       *
       * @attribute useRaw
       * @type boolean
       * @default false
       */
      useRaw: false,
      ready: function() {
        this.load();
      },
      valueChanged: function() {
        this.save();
      },
      load: function() {
        var s = window.localStorage.getItem(this.name);
        if (s && !this.useRaw) {
          this.value = JSON.parse(s);
        } else {
          this.value = s;
        }
      },
      save: function() {
        window.localStorage.setItem(this.name, 
          this.useRaw ? this.value : JSON.stringify(this.value));
      }
    });
  </script>
</polymer-element>
